<!--
 * @Descripttion: 
 * @version: 
 * @Author: ada.wu
 * @Date: 2024-01-05 10:17:42
 * @LastEditors: ada.wu
 * @LastEditTime: 2024-04-09 20:38:46
-->
<template>
  <div class="login-hub">
    <van-empty v-if="isNotAuth" description="暂无权限，请联系管理员…" :image="getAssets('empty.svg')" />
    <template v-if="isLoading">
      <van-skeleton title :row="6" />
      <van-skeleton title :row="6" style="margin-top: 40px" />
    </template>
  </div>
</template>

<script lang="ts">
// import { loginByDebug, loginByWxCode } from "../api/UserApi";
import { useStore } from "../store";
import storageUtil from "../util/storageUtil";
import { requestAuthCode } from "../util/dingUtil";
import { useRoute, useRouter } from "vue-router";
import { onMounted, toRefs, reactive } from "vue";
import { getUserInfoApp } from "/@/api/ScrmApi";

export default {
  name: "Login",
  components: {},
  setup() {
    const route = useRoute();
    const router = useRouter();
    const store = useStore();

    const state = reactive({
      isNotAuth: false,
      isLoading: false,
    });

    const loginAfter = () => {
      // router.replace("/my");
      console.log("loginAfter===已经登陆");
      const redirect = route.query.redirect;
      console.log("loginAfter===", redirect);
      if (redirect) {
        router.replace(decodeURIComponent(String(redirect)));
      } else {
        router.replace("/my");
      }
    };

    /**
     * 是否需要登录
     */
    const checkLogin = () => {
      // storageUtil.removeDingUserInfo()
      let debug = route.query.debug;
      if (debug) {
        store.dispatch("users/getLoginByDebug", {userid:debug}).then(loginAfter);

        // loginByDebug(debug).then((token: any) => {
        //   storageUtil.setToken(token);
        //   console.log("GET_USER_INFO-------2222222");
        //   store.dispatch("users/getUserInfo").then(loginAfter);
        // });
        // return false;
      }
      //已经登录
      //       console.log(
      //         "storageUtil.getCustomerAuthorization() ===",
      //         storageUtil.getCustomerAuthorization()
      //       );
      //       console.log(
      //         "storageUtil.getDingUserInfo() ===",
      //         storageUtil.getDingUserInfo()
      //       );
      //       let userDTO = storageUtil.getDingUserInfo();
      // console.log('========',storageUtil.getCustomerAuthorization() &&
      //         storageUtil.getDingUserInfo())
      //       // store.state.users.userDingDTO
      if (
        storageUtil.getCustomerAuthorization() &&
        storageUtil.getDingUserInfo()
      ) {
        loginAfter();
        console.log("已经登录==");
        return false;
      }
      return true;
    };

    onMounted(async () => {
      // getUserInfoApp({
      //   code: "ecb8e15e793a39d5b7aa69c08be38665",
      // })
      //   .then((res) => {
      //     if (res) {
      //       //       {
      //       //     "associatedUnionid": null,
      //       //     "deviceId": "34aac6d5534bf8a745aeafa7487e6079",
      //       //     "name": "程乔",
      //       //     "sys": false,
      //       //     "sysLevel": 0,
      //       //     "unionid": "kGqGW8FFf7ciShZ1Wwwbz7AiEiE",
      //       //     "userid": "1326093227988585"
      //       // }
      //     }

      //     console.log("res====", res);
      //   })
      //   .catch((err) => {
      //     console.log("err====", err);
      //     state.isNotAuth = true;
      //   })
      //   .finally(() => {
      //     state.isLoading = false;
      //   });
      // }

      //是否需要登录
      if (!checkLogin()) {
        console.log("已经登===");
        return;
      }
      try {
        let code = await requestAuthCode();
        // let code = "783bbe87683d3ada88b4dcac8b633345";
        console.error("code====", code);
        state.isLoading = true;
        if (code) {
          store.dispatch("users/getUserInfo", { code }).then(loginAfter);
        } else {
          state.isNotAuth = true;
        }
        state.isLoading = false;
      } catch (error) {
        console.log("error==", error);
        state.isNotAuth = true;
      }
      // const code = route.query.code;
      // if (code) {
      //   loginByWxCode(code).then((token: any) => {
      //     storageUtil.setToken(token);
      //     console.log("GET_USER_INFO-------3333333");
      //     store.dispatch("users/getUserInfo").then(loginAfter);
      //   });
      // } else {
      //   const redirectUrl = window.location.href;
      //   console.log("GET_USER_INFO-------66666666");
      //   store.dispatch("systems/getSystemInfo").then((conf) => {
      //     wxLogin(conf, redirectUrl);
      //   });
      // }
    });
    return {
      ...toRefs(state),
    };
  },
};
</script>

<style lang="scss" scoped>
.login-hub {
  padding: 80px 0;
  box-sizing: border-box;

  width: 100vw;
  height: 100vh;
  background-color: white;
}
</style>
